home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / tcshsrc.zoo / tcsh / sh.misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-16  |  8.9 KB  |  490 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.00/RCS/sh.misc.c,v 3.4 1991/07/15 19:37:24 christos Exp $ */
  2. /*
  3.  * sh.misc.c: Miscelaneous functions
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "config.h"
  38. RCSID("$Id: sh.misc.c,v 3.4 1991/07/15 19:37:24 christos Exp $")
  39.  
  40. #include "sh.h"
  41.  
  42. #ifdef __MINT__
  43. extern int __mint;    /* kernel version */
  44. #endif
  45.  
  46. static    int    renum    __P((int, int));
  47.  
  48. /*
  49.  * C Shell
  50.  */
  51.  
  52. int
  53. any(s, c)
  54.     register char *s;
  55.     register int c;
  56. {
  57.     if (!s)
  58.     return (0);        /* Check for nil pointer */
  59.     while (*s)
  60.     if (*s++ == c)
  61.         return (1);
  62.     return (0);
  63. }
  64.  
  65. void
  66. setzero(cp, i)
  67.     char   *cp;
  68.     int     i;
  69. {
  70.     if (i != 0)
  71.     do
  72.         *cp++ = 0;
  73.     while (--i);
  74. }
  75.  
  76. char   *
  77. strsave(s)
  78.     register const char *s;
  79. {
  80.     char   *n;
  81.     register char *p;
  82.  
  83.     if (s == NULL)
  84.     s = (const char *) "";
  85.     for (p = (char *) s; *p++;);
  86.     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
  87.     while (*p++ = *s++);
  88.     return (n);
  89. }
  90.  
  91. Char  **
  92. blkend(up)
  93.     register Char **up;
  94. {
  95.  
  96.     while (*up)
  97.     up++;
  98.     return (up);
  99. }
  100.  
  101.  
  102. void
  103. blkpr(av)
  104.     register Char **av;
  105. {
  106.  
  107.     for (; *av; av++) {
  108.     xprintf("%s", short2str(*av));
  109.     if (av[1])
  110.         xprintf(" ");
  111.     }
  112. }
  113.  
  114. int
  115. blklen(av)
  116.     register Char **av;
  117. {
  118.     register int i = 0;
  119.  
  120.     while (*av++)
  121.     i++;
  122.     return (i);
  123. }
  124.  
  125. Char  **
  126. blkcpy(oav, bv)
  127.     Char  **oav;
  128.     register Char **bv;
  129. {
  130.     register Char **av = oav;
  131.  
  132.     while (*av++ = *bv++)
  133.     continue;
  134.     return (oav);
  135. }
  136.  
  137. Char  **
  138. blkcat(up, vp)
  139.     Char  **up, **vp;
  140. {
  141.  
  142.     (void) blkcpy(blkend(up), vp);
  143.     return (up);
  144. }
  145.  
  146. void
  147. blkfree(av0)
  148.     Char  **av0;
  149. {
  150.     register Char **av = av0;
  151.  
  152.     if (!av0)
  153.     return;
  154.     for (; *av; av++)
  155.     xfree((ptr_t) * av);
  156.     xfree((ptr_t) av0);
  157. }
  158.  
  159. Char  **
  160. saveblk(v)
  161.     register Char **v;
  162. {
  163.     register Char **newv =
  164.     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
  165.     Char  **onewv = newv;
  166.  
  167.     while (*v)
  168.     *newv++ = Strsave(*v++);
  169.     return (onewv);
  170. }
  171.  
  172. #ifndef POSIX
  173. char   *
  174. strstr(s, t)
  175.     register const char *s, *t;
  176. {
  177.     do {
  178.     register const char *ss = s;
  179.     register const char *tt = t;
  180.  
  181.     do
  182.         if (*tt == '\0')
  183.         return ((char *) s);
  184.     while (*ss++ == *tt++);
  185.     } while (*s++ != '\0');
  186.     return (NULL);
  187. }
  188.  
  189. #endif /* POSIX */
  190.  
  191. #ifndef SHORT_STRINGS
  192. char   *
  193. strspl(cp, dp)
  194.     char   *cp, *dp;
  195. {
  196.     char   *ep;
  197.     register char *p, *q;
  198.  
  199.     if (!cp)
  200.     cp = "";
  201.     if (!dp)
  202.     dp = "";
  203.     for (p = cp; *p++;);
  204.     for (q = dp; *q++;);
  205.     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
  206.     for (p = ep, q = cp; *p++ = *q++;);
  207.     for (p--, q = dp; *p++ = *q++;);
  208.     return (ep);
  209. }
  210.  
  211. #endif
  212.  
  213. Char  **
  214. blkspl(up, vp)
  215.     register Char **up, **vp;
  216. {
  217.     register Char **wp =
  218.     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
  219.               sizeof(Char **));
  220.  
  221.     (void) blkcpy(wp, up);
  222.     return (blkcat(wp, vp));
  223. }
  224.  
  225. Char
  226. lastchr(cp)
  227.     register Char *cp;
  228. {
  229.  
  230.     if (!cp)
  231.     return (0);
  232.     if (!*cp)
  233.     return (0);
  234.     while (cp[1])
  235.     cp++;
  236.     return (*cp);
  237. }
  238.  
  239. /*
  240.  * This routine is called after an error to close up
  241.  * any units which may have been left open accidentally.
  242.  */
  243. void
  244. closem()
  245. {
  246.     register int f;
  247.  
  248. #ifdef YPBUGS
  249.     /* suggested by Justin Bur; thanks to Karl Kleinpaste */
  250.     fix_yp_bugs();
  251. #endif
  252.     for (f = 0; f < NOFILE; f++)
  253.     if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
  254.         f != FSHTTY)
  255.         (void) close(f);
  256. }
  257.  
  258. #ifndef FIOCLEX
  259. /*
  260.  * Close files before executing a file.
  261.  * We could be MUCH more intelligent, since (on a version 7 system)
  262.  * we need only close files here during a source, the other
  263.  * shell fd's being in units 16-19 which are closed automatically!
  264.  */
  265. void
  266. closech()
  267. {
  268.     register int f;
  269.  
  270. #ifdef __MINT__
  271. /* in TOS, all handles are shared by all processes! (ack!) */
  272.     if (__mint == 0)
  273.     return;
  274. #endif
  275.     if (didcch)
  276.     return;
  277.     didcch = 1;
  278.     SHIN = 0;
  279.     SHOUT = 1;
  280.     SHDIAG = 2;
  281.     OLDSTD = 0;
  282.     isoutatty = isatty(SHOUT);
  283.     isdiagatty = isatty(SHDIAG);
  284.     for (f = 3; f < NOFILE; f++)
  285.     (void) close(f);
  286. }
  287.  
  288. #endif
  289.  
  290. void
  291. donefds()
  292. {
  293.  
  294.     (void) close(0);
  295.     (void) close(1);
  296.     (void) close(2);
  297.     didfds = 0;
  298. }
  299.  
  300. /*
  301.  * Move descriptor i to j.
  302.  * If j is -1 then we just want to get i to a safe place,
  303.  * i.e. to a unit > 2.  This also happens in dcopy.
  304.  */
  305. int
  306. dmove(i, j)
  307.     register int i, j;
  308. {
  309.  
  310.     if (i == j || i < 0)
  311.     return (i);
  312. #ifdef HAVEDUP2
  313. # ifdef __MINT__
  314.     if (__mint != 0)    /* TOS screws up dup2 */
  315. # endif
  316.     if (j >= 0) {
  317.     (void) dup2(i, j);
  318.     if (j != i)
  319.         (void) close(i);
  320.     return (j);
  321.     }
  322. #endif
  323.     j = dcopy(i, j);
  324.     if (j != i)
  325.     (void) close(i);
  326.     return (j);
  327. }
  328.  
  329. int
  330. dcopy(i, j)
  331.     register int i, j;
  332. {
  333.  
  334.     if (i == j || i < 0 || j < 0 && i > 2)
  335.     return (i);
  336. #ifdef __MINT__
  337.     if (__mint == 0 && j > 5) {
  338.     return (i <= 5) ? dup(i) : j;
  339.     }
  340. #endif
  341. #ifdef HAVEDUP2
  342.     if (j >= 0) {
  343.     (void) dup2(i, j);
  344.     return (j);
  345.     }
  346. #endif
  347.     (void) close(j);
  348.     return (renum(i, j));
  349. }
  350.  
  351. static int
  352. renum(i, j)
  353.     register int i, j;
  354. {
  355.     register int k = dup(i);
  356.  
  357.     if (k < 0)
  358.     return (-1);
  359.     if (j == -1 && k > 2)
  360.     return (k);
  361.     if (k != j) {
  362.     j = renum(k, j);
  363.     (void) close(k);
  364.     return (j);
  365.     }
  366.     return (k);
  367. }
  368.  
  369. void
  370. copy(to, from, size)
  371.     register char *to, *from;
  372.     register int size;
  373. {
  374.  
  375.     if (size && from && to)
  376.     do
  377.         *to++ = *from++;
  378.     while (--size != 0);
  379. }
  380.  
  381. /*
  382.  * Left shift a command argument list, discarding
  383.  * the first c arguments.  Used in "shift" commands
  384.  * as well as by commands like "repeat".
  385.  */
  386. void
  387. lshift(v, c)
  388.     register Char **v;
  389.     register int c;
  390. {
  391.     register Char **u = v;
  392.  
  393.     while (*u && --c >= 0)
  394.     xfree((ptr_t) * u++);
  395.     (void) blkcpy(v, u);
  396. }
  397.  
  398. int
  399. number(cp)
  400.     Char   *cp;
  401. {
  402.     if (!cp)
  403.     return (0);
  404.     if (*cp == '-') {
  405.     cp++;
  406.     if (!Isdigit(*cp))
  407.         return (0);
  408.     cp++;
  409.     }
  410.     while (*cp && Isdigit(*cp))
  411.     cp++;
  412.     return (*cp == 0);
  413. }
  414.  
  415. Char  **
  416. copyblk(v)
  417.     register Char **v;
  418. {
  419.     register Char **nv =
  420.     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
  421.  
  422.     return (blkcpy(nv, v));
  423. }
  424.  
  425. #ifndef SHORT_STRINGS
  426. char   *
  427. strend(cp)
  428.     register char *cp;
  429. {
  430.     if (!cp)
  431.     return (cp);
  432.     while (*cp)
  433.     cp++;
  434.     return (cp);
  435. }
  436.  
  437. #endif                /* SHORT_STRINGS */
  438.  
  439. Char   *
  440. strip(cp)
  441.     Char   *cp;
  442. {
  443.     register Char *dp = cp;
  444.  
  445.     if (!cp)
  446.     return (cp);
  447.     while (*dp++ &= TRIM)
  448.     continue;
  449.     return (cp);
  450. }
  451.  
  452. void
  453. udvar(name)
  454.     Char   *name;
  455. {
  456.  
  457.     setname(short2str(name));
  458.     stderror(ERR_NAME | ERR_UNDVAR);
  459. }
  460.  
  461. int
  462. prefix(sub, str)
  463.     register Char *sub, *str;
  464. {
  465.  
  466.     for (;;) {
  467.     if (*sub == 0)
  468.         return (1);
  469.     if (*str == 0)
  470.         return (0);
  471.     if (*sub++ != *str++)
  472.         return (0);
  473.     }
  474. }
  475.  
  476. #ifdef __MINT__
  477. Char *
  478. Lastslash(pth)
  479.     Char *pth;
  480. {
  481.     Char *slash = 0, c;
  482.  
  483.     while ( (c = *pth++) != 0) {
  484.         if (is_dirsep(c))
  485.             slash = pth - 1;
  486.     }
  487.     return slash;
  488. }
  489. #endif /* __MINT__ */
  490.